﻿using System;
using JetBrains.Annotations;
using JetBrains.ReSharper.Psi.Parsing;
using JetBrains.ReSharper.Psi.Xml.Parsing;
using JetBrains.ReSharper.Psi.Xml.Tree;
using JetBrains.Text;

namespace GammaJul.ReSharper.GammaXaml.Psi.Xml {

	/// <summary>
	/// Contains extension methods related to <see cref="IXmlElementFactory"/>.
	/// </summary>
	public static class XmlElementFactoryExtensions {

		/// <summary>
		/// Creates a XML identifier from a string.
		/// </summary>
		/// <param name="elementFactory">A <see cref="IXmlElementFactory"/> used to create the resulting identifier.</param>
		/// <param name="identifier">The identifier as a string.</param>
		/// <param name="xmlTokenTypes">A <see cref="XmlTokenTypes"/> instance.</param>
		/// <returns>A <see cref="IXmlIdentifier"/> representing <paramref name="identifier"/>.</returns>
		[NotNull]
		public static IXmlIdentifier CreateIdentifier([NotNull] this IXmlElementFactory elementFactory, [NotNull] string identifier, [NotNull] XmlTokenTypes xmlTokenTypes) {
			if (elementFactory == null)
				throw new ArgumentNullException("elementFactory");
			if (identifier == null)
				throw new ArgumentNullException("identifier");
			if (xmlTokenTypes == null)
				throw new ArgumentNullException("xmlTokenTypes");

			var buffer = new StringBuffer(identifier);
			return elementFactory.CreateIdentifier(new LexerTokenIntern(), xmlTokenTypes.IDENTIFIER, buffer, 0, buffer.Length);
		}

	}

}